<template>
  <div class="app-container">
    <div class="search-container">
      <el-form :model="searchParam" :inline="true">
        <el-form-item label="标题">
          <el-input
            v-model="searchParam.title"
            placeholder="请输入标题"
            clearable
            @keyup.enter="search"
          />
        </el-form-item>

        <el-form-item label="类型" prop="msgTypeLable">
          <el-select
            v-model="searchParam.msgTypeLable"
            placeholder="请选择类型"
            clearable
          >
            <el-option
              v-for="item in useDictionary('MsgType')"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="发布时间">
          <el-date-picker
            v-model="searchParam.createTimeSearch"
            type="daterange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            value-format="YYYY-MM-DD"
            @change="getcreateTime"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="search"
            ><i-ep-search />搜索</el-button
          >
          <el-button @click="reset"><i-ep-refresh />重置</el-button>
        </el-form-item>
      </el-form>
    </div>

    <el-card shadow="never" class="table-container">
      <el-table
        ref="dataTableRef"
        v-loading="loading"
        :data="tableData"
        highlight-current-row
        border
        @selection-change="handleSelectionChange"
      >
        <el-table-column
          label="标题"
          prop="title"
          min-width="100"
          show-overflow-tooltip
        />
        <!--        <el-table-column label="消息类型" prop="msgTypeLable" min-width="100" />-->
        <el-table-column
          label="消息内容"
          prop="msgBody"
          min-width="100"
          show-overflow-tooltip
        />
        <el-table-column
          label="发送人姓名"
          prop="createdUserName"
          min-width="100"
          show-overflow-tooltip
        />
        <el-table-column
          label="发布时间"
          prop="sendTime"
          min-width="100"
          show-overflow-tooltip
        />
        <!-- <el-table-column
          label="接收人id"
          prop="recevieUserId"
          min-width="100"
        /> -->
        <el-table-column
          label="阅读时间"
          prop="readTime"
          min-width="100"
          show-overflow-tooltip
        />
        <el-table-column label="是否已读" align="center" width="100">
          <template #default="scope">
            <el-tag v-if="scope.row.readed === 1" type="success">已读</el-tag>
            <el-tag v-else type="info">未读</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="备注" prop="remarks" min-width="100" />

        <el-table-column fixed="right" label="操作" width="240">
          <template #default="scope">
            <el-button
              type="primary"
              size="small"
              link
              @click="openDialog(scope.row)"
              ><i-ep-edit />详情</el-button
            >
          </template>
        </el-table-column>
      </el-table>

      <el-pagination
        small
        :current-page="pageable.pageNum"
        :page-size="pageable.pageSize"
        :page-sizes="[10, 25, 50, 100]"
        :total="pageable.total"
        layout="total, sizes, prev, pager, next, jumper"
        class="mt-[20px]"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </el-card>

    <sysmessage-add-or-update
      :visible="addDialog.visible"
      :title="addDialog.title"
      :data="addDialog.data"
      @reset-table="reset"
      @change-visible="changeVisible"
    />
  </div>
</template>

<script setup lang="ts">
import { useTable, useDictionary } from "@/utils/useTable";
import request from "@/utils/request";
import { useUserStoreHook } from "@/store/modules/user";
import { storeToRefs } from "pinia";

const userStore = useUserStoreHook();
const { linkId } = storeToRefs(userStore);
defineOptions({
  name: "WORKBENCHSYSMESSAGE",
  inheritAttrs: false,
});

// 请求参数
const requestApi = "/system/sysmessage/queryByPage";

// 是否需要分页
const pagination = true;

const initParam = {};

// 初始化搜索参数数据
const searchParamInit = {
  title: "",
  msgType: "",
  createTimeSearch: [],
  sendTimeStart: "",
  sendTimeEnd: "",
};

const deleteParam = {
  api: "/system/sysmessage/delete",
  data: {},
};

// 数据请求错误后处理
function requestError() {}

const dataCallBack = ref(undefined); // 无需二次数据处理&不传赞数也行

// 表格操作 Hooks
const {
  tableData,
  pageable,
  searchParam,
  ids,
  getTableList,
  search,
  reset,
  handleSizeChange,
  handleCurrentChange,
  handleSelectionChange,
  handleTableDelete,
  loading,
}: any = useTable(
  requestApi,
  initParam,
  searchParamInit,
  pagination,
  dataCallBack.value,
  requestError,
  deleteParam
);

onMounted(() => {
  getTableList();
  // 缓冲之后，当pinia中存在linkId，则呼出详情弹窗
  setTimeout(() => {
    if (linkId.value) {
      openDialog({ id: linkId.value });
      userStore.setLinkId("");
    }
  }, 300);
});

const addDialog = reactive<DialogOption>({
  visible: false,
  title: "",
  data: {},
});

// 查询详情
async function getInfo(id: string) {
  const { data } = await request({
    url: `/system/sysmessage/get/${id}`,
    method: "get",
  });
  addDialog.data = data;
  // 更新未读条数
  userStore.getUnReadMessage();
}

/** 打开表单弹窗 */
function openDialog(roleObj?: Object) {
  if (roleObj) {
    addDialog.title = "消息详情";
    getInfo(roleObj.id);
  } else {
    addDialog.title = "新增";
  }
  addDialog.visible = true;
}

function changeVisible(value: boolean) {
  addDialog.visible = value;
  if (!value) {
    getTableList();
    addDialog.data = {};
  }
}

function getcreateTime(data: any) {
  if (data) {
    searchParam.value.sendTimeStart = data[0];
    searchParam.value.sendTimeEnd = data[1];
  } else {
    searchParam.value.sendTimeStart = "";
    searchParam.value.sendTimeEnd = "";
  }
}
</script>
